<?php

if (is_numeric($_POST['parent_id'])) {
	$sort = $db->fetchOne("SELECT MAX(sort) FROM site_pages WHERE parent_id=?", $_POST['parent_id']) + 10;
	$db->insert('site_pages', array('site_id' => $site['id'], 'parent_id' => $_POST['parent_id'], 'sort' => $sort));
	$page_id = $db->lastInsertId();
	$default_template_id = $db->fetchOne("SELECT id FROM site_templates ORDER BY sort LIMIT 1");
	$db->insert('site_pages_revisions', array('title' => 'Untitled page', 'page_id' => $page_id, 'user_id' => $_SESSION['login']['user']['id'], 'template_id' => $default_template_id));
	$db->update('site_pages', array('revision_id' => $db->lastInsertId()), "id={$page_id}");
	header('Content-type: application/json');
	echo json_encode(array('id' => $page_id));
}
